package com.ssm.zy.service.impl;

import com.ssm.zy.mapper.CommentMapper;
import com.ssm.zy.mapper.PostMapper;
import com.ssm.zy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private PostMapper postMapper;

    public int commentComment(Integer postId, Integer commentId, Integer userId, String commentContent) {
        Date date = new Date();
        return commentMapper.addCommentComment(postId,commentId,userId,commentContent,date);
    }

    @Transactional
    public int praiseComment(Integer commentId, Integer userId) {
        List<Integer> userIdList=postMapper.getPraiseOfUserComment(commentId);
        int i=0;
        int j=0;
        if(userIdList.contains(userId)){//包含说明已经点过赞，需要取消点赞
            i=commentMapper.delpraiseComment(commentId,userId);
            j=commentMapper.delpraiseCommentopreation(commentId);
        }else{//不包含说明没有点过
            i=commentMapper.praiseComment(commentId,userId);
            j=commentMapper.praiseCommentopreation(commentId);
        }
        if(i>0 && j>0){
            return i+j;
        }
        return 0;
    }

    @Override
    public int deleteUserPostComment(int commentId) {
        return commentMapper.deleteUserPostComment(commentId);
    }
}
